package com.sizu.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sizu.common.dto.UserDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TUser;
import com.sizu.entity.TUserRole;
import com.sizu.service.ITUserRoleService;
import com.sizu.service.ITUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

/**
 * @author 高旌洋
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private ITUserService userService;

    @Autowired
    private ITUserRoleService userRoleService;

    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * 返回用户信息
     *
     * @return
     */
    @RequestMapping("/info")
    public ResultEntity getUserInfo(Principal member) {
        return ResultEntity.ok(member);
    }

    @RequestMapping("/userList")
    public ResultEntity userList(UserDto userDto) {
        log.info("userDto.getTrueName()-->{}", userDto.getTrueName());
        List<TUser> userAndRoleList = userService.getUserAndRoleList(userDto);
        return ResultEntity.ok(userAndRoleList);
    }

    /**
     * 添加用户角色信息
     *
     * @param userDto
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResultEntity save(UserDto userDto) {
        log.info("userDto-->{}", userDto);

        TUser user = new TUser();
        BeanUtils.copyProperties(userDto, user);
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        log.info("encode {}", encode);
        userService.insertOrUpdate(user);
        TUser user1 = userService.selectById(user.getId());
        if (null != userDto.getRid() && userDto.getRid().length != 0) {
            for (Integer integer : userDto.getRid()) {
                TUserRole userRole = new TUserRole();
                userRole.setUserId(user1.getId());
                userRole.setRoleId(integer);
                userRoleService.insert(userRole);
            }
        }

        return ResultEntity.ok();
    }

    /**
     * 根据ID删除用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/delById")
    public ResultEntity delById(Integer id) {
        System.err.println(id);
        TUser user = userService.selectById(id);
        if (user.getId() == null) {
            return ResultEntity.error("401", "请选择要删除的用户", null);
        }
        userRoleService.delByUserId(user.getId());
        userService.deleteById(user.getId());

        return ResultEntity.ok("200", "删除成功", null);
    }

    /**
     * 修改密码
     *
     * @param userDto
     * @return
     */
    @RequestMapping("/updatepassword")
    public ResultEntity updatePass(@RequestBody UserDto userDto) {
        log.info("userDto()---> {}", userDto);
        TUser byUserName = userService.findByUserName(userDto);
        String encode = passwordEncoder.encode(userDto.getPassword());
        log.info("encode {}", encode);
        byUserName.setPassword(encode);
        byUserName.setRemarks(userDto.getRemarks());
        userService.insertOrUpdate(byUserName);
        return ResultEntity.ok("200", "修改密码成功", null);
    }

    /**
     * 修改角色
     *
     * @param userDto
     * @return
     */
    @RequestMapping("/userrole")
    public ResultEntity userRole(UserDto userDto) {
        Wrapper<TUserRole> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userDto.getId());
        userRoleService.delete(wrapper);
        if (null != userDto.getRid() && userDto.getRid().length != 0) {
            for (Integer integer : userDto.getRid()) {
                TUserRole userRole = new TUserRole();
                userRole.setUserId(userDto.getId());
                userRole.setRoleId(integer);
                userRoleService.insert(userRole);
            }
        }
        return ResultEntity.ok("修改成功");
    }
}
